package cn.iune.web.erp.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.iune.config.SecurityService;
import cn.iune.erp.customer.FileCustomer;
import cn.iune.erp.customer.ICustomerItemService;
import cn.iune.erp.customer.IFileCustomerGroupService;
import cn.iune.erp.customer.IFileCustomerService;
import cn.iune.erp.customer.IMemberLevelService;
import cn.iune.erp.customer.IMemberPointsService;



@Controller
@RequestMapping(value="/customer")
public class CustomerController {
	
	@Autowired
	private IFileCustomerService fileCustomerService;
	@Autowired
	private ICustomerItemService customerItemService;
	
	@Autowired
	private IFileCustomerGroupService fileGroupService;

	@Autowired
	private IMemberLevelService memberLevelService;
	@Autowired
	private IMemberPointsService memberPointsService;
	
	
	@RequestMapping(value="/customerInfo/single",method=RequestMethod.GET)
	public ModelAndView customerSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/customer/customerInfo/single");
		return mav;
	}

	
	
	@RequestMapping(value={"/customerInfo/single/query"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject customerQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return fileCustomerService.queryData(data);
	}

	@RequestMapping(value="/customerInfo/single/save",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject customerSave(@RequestBody JSONObject object) {
		return fileCustomerService.saveData(object);
	}

	@RequestMapping(value="/customerInfo/single/delete",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject customerDelete(@RequestBody JSONObject data) {
		return fileCustomerService.deleteData(data);
	}

	@RequestMapping(value={"/customerInfo/single/create"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject customerCreate(@RequestBody JSONObject data) {
		data = SecurityService.translateFun(data);
		JSONObject json = fileCustomerService.newData(data);
		return json;
	}

	@RequestMapping(value={"/customerInfo/single/createDetail"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject customerCreateDetail(@RequestBody JSONObject data) {
		JSONObject json = fileCustomerService.newDataDetail(data);
		return json;
	}

	@RequestMapping(value="/customerInfo/single/queryDetail",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject customerQueryDetail(@RequestBody JSONObject data) {
		return fileCustomerService.queryDataDetail(data);
	}


	
	@RequestMapping(value="/customerInfo/selectSingle",method=RequestMethod.GET)
	public ModelAndView customerSelectSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/customer/customerInfo/selectSingle");
		return mav;
	}

	@RequestMapping(value={"/customerInfo/selectSingle/query"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject customerQueryUnion(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return fileCustomerService.queryUNION(data);
	}
	

	//根据客户编码获取客户信息
	@RequestMapping(value={"/getCustomerByNo"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject getCustomerByNo(@RequestBody JSONObject data){
		String customerNo = data.getString("customerNo");
//		String enterpriseNo = data.getString("enterpriseNo");
		String enterpriseNo = SecurityService.getCurrentOgn().getsFID();
		JSONObject json = new JSONObject();
		FileCustomer fileCustomer = fileCustomerService.getByNo(customerNo, enterpriseNo);
		
		if(fileCustomer==null){
			//客户不存在
			json.put("code", -1);
			json.put("status", "编码为：‘"+customerNo+"’的客户不存在");
		}else{
			//客户存在
			json.put("code", 0);
			json.put("status", "操作成功");
			json.put("data", fileCustomer);
		}
		
		/*
		List<FileCustomer> fileCustomers = fileCustomerService.getByNo(customerNo, enterpriseNo);
		if(fileCustomers.size()<=0){
			//客户不存在
			json.put("code", -1);
			json.put("status", "编码为：‘"+customerNo+"’的客户不存在");
		}else if(fileCustomers.size()>1){
			json.put("code", 1);
			json.put("status", "编码为：‘"+customerNo+"’的客户多于1个");
			json.put("count", fileCustomers.size());
		}else{
			//客户存在
			json.put("code", 0);
			json.put("status", "操作成功");
			json.put("data", fileCustomers.get(0));
		}
		*/

		return json;
	}
	
	//获取会员余额
	@RequestMapping(value={"/getMemberAmount"},method=RequestMethod.GET)
	@ResponseBody
	public JSONObject getMemberAmount(@RequestParam String customerID) {
		return fileCustomerService.getMemberAmount(customerID);
	}


	
	
	
	
	
	//分类
	@RequestMapping(value="/customerGroup/tree",method=RequestMethod.GET)
	public ModelAndView customerGroupTreeView(){
		ModelAndView mav = new ModelAndView("/appcenter/customer/customerGroup/tree");
		return mav;
	}
	
	//重新排序
	@RequestMapping(value="/customerGroup/sortView",method=RequestMethod.GET)
	public ModelAndView customerGroupSortView(){
		ModelAndView mav = new ModelAndView("/appcenter/customer/customerGroup/sort");
		return mav;
	}
	
	//移动
	@RequestMapping(value="/customerGroup/moveView",method=RequestMethod.GET)
	public ModelAndView customerGroupMoveView(){
		ModelAndView mav = new ModelAndView("/appcenter/customer/customerGroup/move");
		return mav;
	}
	
	//选择物料分类（单选）
	@RequestMapping(value="/customerGroup/selectSingle",method=RequestMethod.GET)
	public ModelAndView customerGroupSelectSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/customer/customerGroup/selectSingle");
		return mav;
	}
	
	@RequestMapping(value={
			"/customerGroup/tree/create"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject customerGroupCreate(@RequestBody JSONObject data) {
		data = SecurityService.translateFun(data);
		JSONObject json = fileGroupService.newData(data);
		return json;
	}

	@RequestMapping(value= {
			"/customerGroup/tree/query"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject customerGroupQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return fileGroupService.queryData(data);
	}

	@RequestMapping(value= {
			"/customerGroup/tree/save"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject customerGroupSave(@RequestBody JSONObject object) {
		return fileGroupService.saveData(object);
	}
	
	@RequestMapping(value= {
			"/customerGroup/tree/delete"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject customerGroupDelete(@RequestBody JSONObject data) {
		return fileGroupService.deleteData(data);
	}

	
	@RequestMapping(value= {
			"/customerGroup/tree/sort"
			},method=RequestMethod.POST)
	@ResponseBody
	public void customerGroupSort(@RequestBody JSONObject data){
		String parentID = data.getString("parentID");
		JSONArray ids =  data.getJSONArray("ids");
		fileGroupService.sort(parentID, ids);
	}
	
	@RequestMapping(value= {
			"/customerGroup/tree/move"
			},method=RequestMethod.POST)
	@ResponseBody
	public void customerGroupMove(@RequestBody JSONObject data){
		String rowID = data.getString("rowID");
		String oldParentID = data.getString("oldParentID");
		String newParentID = data.getString("newParentID");
		fileGroupService.move(rowID, oldParentID, newParentID);
	}
	
	
	
	
	
	
	
	
	@RequestMapping(value="/customerItem/single",method=RequestMethod.GET)
	public ModelAndView customerItemSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/customer/customerItem/single");
		return mav;
	}

	
	
	@RequestMapping(value={"/customerItem/single/query"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject customerItemQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return customerItemService.queryData(data);
	}

	@RequestMapping(value="/customerItem/single/save",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject customerItemSave(@RequestBody JSONObject object) {
		return customerItemService.saveData(object);
	}

	@RequestMapping(value="/customerItem/single/delete",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject customerItemDelete(@RequestBody JSONObject data) {
		return customerItemService.deleteData(data);
	}

	@RequestMapping(value={"/customerItem/single/create"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject customerItemCreate(@RequestBody JSONObject data) {
		data = SecurityService.translateFun(data);
		JSONObject json = customerItemService.newData(data);
		return json;
	}

	
	
	@RequestMapping(value="/memberLevel/list",method=RequestMethod.GET)
	public ModelAndView memberLevelList(){
		ModelAndView mav = new ModelAndView("/appcenter/customer/memberLevel/list");
		return mav;
	}

	@RequestMapping(value="/memberLevel/single",method=RequestMethod.GET)
	public ModelAndView memberLevelSingle(){
	
		ModelAndView mav = new ModelAndView("/appcenter/customer/memberLevel/single");
		return mav;
	}

	@RequestMapping(value="/memberLevel/selectSingle",method=RequestMethod.GET)
	public ModelAndView memberLevelSelectSingle(){
	
		ModelAndView mav = new ModelAndView("/appcenter/customer/memberLevel/selectSingle");
		return mav;
	}

	@RequestMapping(value={"/memberLevel/list/create"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject memberLevelCreate(@RequestBody JSONObject data) {
		data = SecurityService.translateFun(data);
		JSONObject json = memberLevelService.newData(data);
		return json;
	}

	@RequestMapping(value="/memberLevel/list/query",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject memberLevelQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return memberLevelService.queryData(data);
	}

	@RequestMapping(value="/memberLevel/list/save",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject memberLevelSave(@RequestBody JSONObject object) {
		return memberLevelService.saveData(object);
	}

	@RequestMapping(value="/memberLevel/list/delete",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject memberLevelDelete(@RequestBody JSONObject data) {
		return memberLevelService.deleteData(data);
	}
	
	
	
	@RequestMapping(value="/memberPoints/list",method=RequestMethod.GET)
	public ModelAndView memberPointsList(){
		ModelAndView mav = new ModelAndView("/appcenter/customer/memberPoints/list");
		return mav;
	}

	@RequestMapping(value="/memberPoints/single",method=RequestMethod.GET)
	public ModelAndView memberPointsSingle(){
	
		ModelAndView mav = new ModelAndView("/appcenter/customer/memberPoints/single");
		return mav;
	}

	@RequestMapping(value="/memberPoints/selectSingle",method=RequestMethod.GET)
	public ModelAndView memberPointsSelectSingle(){
	
		ModelAndView mav = new ModelAndView("/appcenter/customer/memberPoints/selectSingle");
		return mav;
	}

	@RequestMapping(value={"/memberPoints/list/create"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject memberPointsCreate(@RequestBody JSONObject data) {
		data = SecurityService.translateFun(data);
		JSONObject json = memberPointsService.newData(data);
		return json;
	}

	@RequestMapping(value="/memberPoints/list/query",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject memberPointsQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return memberPointsService.queryData(data);
	}

	@RequestMapping(value="/memberPoints/list/save",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject memberPointsSave(@RequestBody JSONObject object) {
		return memberPointsService.saveData(object);
	}

	@RequestMapping(value="/memberPoints/list/delete",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject memberPointsDelete(@RequestBody JSONObject data) {
		return memberPointsService.deleteData(data);
	}

	


}
